<?php 


class Controller_Feature extends Controller_Website {

   //人才
    public function action_person_list() {
        $m_feature = Model::factory('Feature');
        $where = array(
            'person_cid'=>array('!='=>0)
        );
        $total = $m_feature->count($where);
        $pager = new Pager($total, 10);
        $feature = $m_feature->select($pager->offset, $pager->size, $where)->as_array();
        
        $m_personCategory = Model::factory('personCategory');
        $personCategory = $m_personCategory->getAll()->as_array();
        
        foreach ($feature as &$item) {
            foreach ($personCategory as $item2) {
                if ($item['person_cid'] == $item2['id']) {
                    $item['person_cname'] = $item2['name'];
                }
            }
        }
//         var_dump($feature);exit;
        $this->content = View::factory('feature/person_list');
        $this->content->feature = $feature;
        $this->content->pager = $pager;
    }
    
    public function action_person_add() {
        $m_personCategory = Model::factory('personCategory');
        $m_feature = Model::factory('Feature');
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $data['add_time'] = time();
            $ret = $m_feature->insert($data);
            if ($ret !== false) {
                $this->redirect('feature/person_list');
            }
        }
        
        $cat_list = $m_personCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        
    
    
    
        $this->content = View::factory('feature/person_add');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->info = null;
    }
    
    
    

    protected function _get_data($post) {
        return array_intersect_key($post, array_flip(array('person_cid','tag')));
    }
    
    //设备
    public function action_device_list() {
        $m_feature = Model::factory('Feature');
        $where = array(
            'device_brands_id'=>array('!='=>0)
        );
        $total = $m_feature->count($where);
        $pager = new Pager($total, 10);
        $feature = $m_feature->select($pager->offset, $pager->size, $where)->as_array();
        
        $m_deviceBrands = Model::factory('deviceBrands');
        $deviceBrands = $m_deviceBrands->getAll()->as_array();
        
        $m_deviceCategory = Model::factory('deviceCategory');
        $deviceCategory = $m_deviceCategory->getAll()->as_array();
        
        foreach ($deviceBrands as &$item) {
            foreach ($deviceCategory as $item2) {
                if ($item['device_cid'] == $item2['id']) {
                    $item['device_cname'] = $item2['name'];
                }
            }
        }
        
        foreach ($feature as &$item) {
            foreach ($deviceBrands as $item2) {
                    if ($item['device_brands_id'] == $item2['id']) {
                        $item['brands_name'] = $item2['name'];
                        $item['device_cname'] = $item2['device_cname'];
                    }
            }
        }

        
        
//         var_dump($feature);exit;
    
        $this->content = View::factory('feature/device_list');
        $this->content->feature = $feature;
        $this->content->pager = $pager;
    }
    
    public function action_device_add() {
        $m_deviceCategory = Model::factory('deviceCategory');
        $m_feature = Model::factory('Feature');
        $m_deviceBrands = Model::factory('deviceBrands');
        
        if (!empty($_POST)) {
            $data = $this->_get_data2($_POST);
            $data['add_time'] = time();
            $ret = $m_feature->insert($data);
            if ($ret !== false) {
                $this->redirect('feature/device_list');
            }
        }
    
        $cat_list = $m_deviceCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $deviceBrands = $m_deviceBrands->getAll()->as_array();
    
        $this->content = View::factory('feature/device_add');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->deviceBrands = $deviceBrands;
        $this->content->info = null;
    }
    
    
    
    
    protected function _get_data2($post) {
        return array_intersect_key($post, array_flip(array('device_brands_id','tag')));
    }
    
    //场地
    public function action_place_list() {
        $m_feature = Model::factory('Feature');
        $where = array(
            'place_cid'=>array('!='=>0)
        );
        $total = $m_feature->count($where);
        $pager = new Pager($total, 10);
        $feature = $m_feature->select($pager->offset, $pager->size, $where)->as_array();
    
        $m_placeCategory = Model::factory('placeCategory');
        $placeCategory = $m_placeCategory->getAll()->as_array();
    
        foreach ($feature as &$item) {
            foreach ($placeCategory as $item2) {
                if ($item['place_cid'] == $item2['id'] && $item['place_cid'] != 0) {
                    $item['place_cname'] = $item2['name'];
                }
            }
        }
//     var_dump($feature);exit;
        $this->content = View::factory('feature/place_list');
        $this->content->feature = $feature;
        $this->content->pager = $pager;
    }
    
    public function action_place_add() {
        $m_placeCategory = Model::factory('placeCategory');
        $m_feature = Model::factory('Feature');
    
        if (!empty($_POST)) {
            $data = $this->_get_data3($_POST);
            $data['add_time'] = time();
            $ret = $m_feature->insert($data);
            if ($ret !== false) {
                $this->redirect('feature/place_list');
            }
        }
    
        $cat_list = $m_placeCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
    
    
    
    
    
        $this->content = View::factory('feature/place_add');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->info = null;
    }
    
    
    
    
    protected function _get_data3($post) {
        return array_intersect_key($post, array_flip(array('place_cid','tag')));
    }
    
    public function action_liandong() {
        $device_cid = Arr::get($_POST, 'device_cid');
        
        $m_deviceBrands = Model::factory('deviceBrands');
        
        $deviceBrands = $m_deviceBrands->getAll(array('device_cid'=>$device_cid))->as_array();
        
        echo json_encode($deviceBrands);
    }
}

